#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <sstream>

using namespace std;

int main() {
    int numNodes;
    cin >> numNodes;

    vector<char> nodes(numNodes);
    for (int i = 0; i < numNodes; ++i) {
        cin >> nodes[i];
    }

    cin.ignore(); // 忽略换行符

    // 构建邻接表
    unordered_map<char, vector<char>> adjacencyList;
    string line;
    while (getline(cin, line)) {
        if (line.empty()) break; // 遇到空行停止读取
        istringstream iss(line);
        char u, v;
        iss >> u >> v;
        adjacencyList[u].push_back(v);
        adjacencyList[v].push_back(u); // 无向图，需要双向添加
    }

    // 广度优先搜索
    queue<char> q;
    unordered_set<char> visited;
    vector<char> bfsOrder;

    q.push(nodes[0]);
    visited.insert(nodes[0]);

    while (!q.empty()) {
        char currentNode = q.front();
        q.pop();
        bfsOrder.push_back(currentNode);

        for (char neighbor : adjacencyList[currentNode]) {
            if (visited.find(neighbor) == visited.end()) {
                q.push(neighbor);
                visited.insert(neighbor);
            }
        }
    }

    // 输出遍历序列
    for (size_t i = 0; i < bfsOrder.size(); ++i) {
        cout << bfsOrder[i];
        if (i < bfsOrder.size() - 1) {
            cout << " ";
        }
    }
    cout << endl;

    return 0;
}